package com.example.demo.core;

import com.example.demo.model.Admin;
import com.example.demo.model.User;
import org.springframework.cglib.beans.BeanMap;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Response {
    public static Object ok() {
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("errno", 0);
        obj.put("errmsg", "成功");
        return obj;
    }

    public static Object okList(List list,int pageNumber,int limit) {
        Map<String, Object> data = new HashMap<String, Object>();
        Page page = new Page(list,pageNumber,limit);
        data.put("total", page.getTotal());
        data.put("page", page.getPageNum());
        data.put("limit", page.getPageSize());
        data.put("pages", page.getPages());
        data.put("list", page.getListPage());
        return ok(data);
    }

    public static Object ok(Object data) {
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("errno", 0);
        obj.put("errmsg", "成功");
        obj.put("data", data);
        return obj;
    }

    public static Object Update_ok(int number) {
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("errno", 0);
        obj.put("errmsg", "成功");
        obj.put("ChangedCnt", number);
        return obj;
    }

    public static Object Insert_ok(int number) {
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("errno", 0);
        obj.put("errmsg", "成功");
        obj.put("InsertCnt", number);
        return obj;
    }

    public static Object Detele_ok(int number) {
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("errno", 0);
        obj.put("errmsg", "成功");
        obj.put("DeleteCnt", number);
        return obj;
    }

    /**
     * 把一个对象转换为Map
     * @param bean
     * @param <T>
     * @return
     */
    public static <T> Map<String, Object> objectToMap(T bean) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                map.put(key.toString(), beanMap.get(key));
            }
        }
        return map;
    }

    /**
     * 把list内的对象转换为Map，list也转换为map
     * @param list
     * @return
     */
    public static Object objectListToMapList(List list)
    {
        Map<String, Object> obj = new HashMap<String, Object>();
        for (int i=1;i<=list.size();i++)
        {
            obj.put(String.valueOf(i),objectToMap(list.get(i-1)));
        }
        return obj;
    }

    public static Object fail() {
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("errno", -1);
        obj.put("errmsg", "错误");
        return obj;
    }

    public static Object fail(int errno, String errmsg) {
        Map<String, Object> obj = new HashMap<String, Object>();
        obj.put("errno", errno);
        obj.put("errmsg", errmsg);
        return obj;
    }

    public static Object fail(int errno, String errmsg, String data) {
        Map<String, Object> obj = new HashMap<String, Object>(3);
        obj.put("errno", errno);
        obj.put("errmsg", errmsg);
        obj.put("data", data);
        return obj;
    }

    public static Object badArgument() {
        return fail(401, "参数不对");
    }

    public static Object badArgumentValue() {
        return fail(402, "参数值不对");
    }

    public static Object badArgumentValue(String param) {
        return fail(402, String.format("参数值%s不对",param));
    }

    public static Object unlogin() {
        return fail(507, "请登录");
    }


    public static Object serious() {
        return fail(502, "系统内部错误");
    }

    public static Object unsupport() {
        return fail(503, "业务不支持");
    }

    public static Object updatedDateExpired() {
        return fail(504, "更新数据已经失效");
    }

    public static Object updatedDataFailed() {
        return fail(505, "更新数据失败");
    }

    public static Object unauthz() {
        return fail(506, "未授权");
    }

    /**
     * test
     * @param args
     */
    public static void main(String[] args) {
        List list = new LinkedList();
        IntStream.range(1,2).forEach(list::add);
        System.out.println(okList(list,1,5));
    }
}
